package com.huayun.weexutil;

import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;


import com.huayun.base.BaseActivity;
import com.huayun.util.utils.LogUtil;
import com.huayun.util.utils.ToastUtil;
import com.huayun.util.utils.Utils;
import com.taobao.weex.ui.view.WXScrollView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Description:
 * Created on 2019/8/2 0002 15:57:49
 * author:Ahuangshang
 */

public class WeexActivity extends BaseActivity {
    private WeexUtil weexUtil;
    private FrameLayout viewGroup;
    private String pageName;//要加载的js的名称
    private String pageTitle;//要加载页面的标题
    private boolean showToolBar;//是否显示标题栏
    private boolean lightStatusBar;//顶部系统状态栏字体颜色是否清楚黑色，默认顶部状态栏是黑色的，如果该值设为true,则显示为白色
    Uri uri;
    private WXScrollView scrollView;
    private Map<String, Object> options;
    private String optionsJsonString;//由于js到原生还有Intent跳转关系，不能直接传递Map，故通过字符串传递

    @Override
    protected void bindView() {
        uri = getIntent().getData();
        if (uri != null) {
            LogUtil.e("---------->" + uri.toString());
            weexUtil = new WeexUtil(uri.toString(), viewGroup, this);
        } else {
            pageName = getIntent().getStringExtra("pageName");
            if (!TextUtils.isEmpty(pageName)) {
                weexUtil = new WeexUtil(pageName, options, viewGroup, this);
            } else {
                ToastUtil.showMessage("页面路劲不能为空");
                return;
            }
        }
        setToolBar();
    }

    private void setToolBar() {
        pageTitle = getIntent().getStringExtra("pageTitle");
        setTitle(TextUtils.isEmpty(pageTitle) ? TextUtils.isEmpty(pageName) ? uri.getLastPathSegment() : pageName : pageTitle);
        showToolBar = getIntent().getBooleanExtra("showToolBar", true);
        getToolbar().setVisibility(showToolBar ? View.VISIBLE : View.GONE);
        lightStatusBar = getIntent().getBooleanExtra("lightStatusBar", false);
        if (lightStatusBar) {
            setStatusBarLightModel();
        }
    }

    @Override
    protected void initView() {
        viewGroup = findViewById(R.id.weex_group);
    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        optionsJsonString = getIntent().getStringExtra("optionsJsonString");
        if (optionsJsonString != null) {
            options = Utils.jsonToMap(optionsJsonString);
        }
    }

    @Override
    protected int getContentViewLayoutResId() {
        return R.layout.activity_weex;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (weexUtil != null) {
            weexUtil.destroy();
        }
        if (viewGroup != null) {
            viewGroup.removeAllViews();
        }
        viewGroup = null;
    }

    @Override
    public void call(Object o) {
        super.call(o);
        getScrollerView(viewGroup);
        if (scrollView != null) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    View childAt = scrollView.getChildAt(0);
                    int height = childAt.getMeasuredHeight();
                    scrollView.smoothScrollBy(0, height);
                    //scrollView.fullScroll(ScrollView.FOCUS_DOWN);//这个方法会将焦点回到最底部，多个输入框时有问题
                }
            }, 90);
        }

    }

    private void getScrollerView(View view) {
        List<View> allChildViews = getAllChildViews(view);
        for (int i = 0; i < allChildViews.size(); i++) {
            if (allChildViews.get(i) instanceof WXScrollView) {
                scrollView = (WXScrollView) allChildViews.get(i);
                break;
            }
        }
    }

    private List<View> getAllChildViews(View view) {
        List<View> allchildren = new ArrayList<View>();
        if (view instanceof ViewGroup) {
            ViewGroup vp = (ViewGroup) view;
            for (int i = 0; i < vp.getChildCount(); i++) {
                View viewchild = vp.getChildAt(i);
                allchildren.add(viewchild);
                //再次 调用本身（递归）
                allchildren.addAll(getAllChildViews(viewchild));
            }
        }
        return allchildren;
    }
}
